package net.ddyule.www.ddyule;

import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.BackgroundColorSpan;
import android.util.Log;
import com.tencent.smtt.sdk.ValueCallback;
import android.widget.EditText;
import android.widget.Toast;

import com.iflytek.cloud.ErrorCode;
import com.iflytek.cloud.InitListener;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechEvent;
import com.iflytek.cloud.SpeechSynthesizer;
import com.iflytek.cloud.SpeechUtility;
import com.iflytek.cloud.SynthesizerListener;
import com.iflytek.sunflower.FlowerCollector;

import static android.content.Context.MODE_PRIVATE;

public class tts_xunfei {
    private Toast mToast;
    // 语音合成对象
    private SpeechSynthesizer mTts;
    // 默认发音人
    public String voicer = "xiaoyan";
    // 引擎类型
    private String mEngineType = SpeechConstant.TYPE_CLOUD;
    //音量
    private int volume = 50;
    //音调
    private int pitch = 50;
    //语速
    private int speed = 50;

    String texts = "";
    private static String TAG = MainActivity.class.getSimpleName();

    public void init(){
        mToast = Toast.makeText(MainActivity.sContent,"",Toast.LENGTH_SHORT);
        // 初始化合成对象
        mTts = SpeechSynthesizer.createSynthesizer(MainActivity.sContent, mTtsInitListener);
        if(mTts == null)
        {
            showTip("初始化失败");
        }

        setParam();

    }

    public void speak(String text)
    {
       // FlowerCollector.onEvent(MainActivity.sContent, "tts_play");

        // 设置参数
        setParam();
        int code = mTts.startSpeaking(text, mTtsListener);
//			/**
//			 * 只保存音频不进行播放接口,调用此接口请注释startSpeaking接口
//			 * text:要合成的文本，uri:需要保存的音频全路径，listener:回调接口
//			*/
			/*String path = Environment.getExternalStorageDirectory()+"/tts.pcm";
			int code = mTts.synthesizeToUri(text, path, mTtsListener);*/

        if (code != ErrorCode.SUCCESS) {
            showTip("语音合成失败,错误码: " + code);
        }
    }

    public void stop()
    {
        mTts.stopSpeaking();
    }

    public void pause()
    {
        mTts.pauseSpeaking();
    }

    public void resume(){
        mTts.resumeSpeaking();
    }

    public boolean isSpeaking(){
        return mTts.isSpeaking();
    }

    public void setSpeaker(String string) {
        voicer = string;
        setParam();
    }

    public void setPitch(int pitch) {
        this.pitch = pitch;
        setParam();
    };

    public void setVolume(int volume){
        this.volume = volume;
        setParam();
    }

    public void setSpeed(int speed){
        this.speed = speed;
        setParam();
    }

    /**
     * 初始化监听。
     */
    private InitListener mTtsInitListener = new InitListener() {
        @Override
        public void onInit(int code) {
            Log.d(TAG, "InitListener init() code = " + code);
            if (code != ErrorCode.SUCCESS) {
                showTip("初始化失败,错误码："+code);
            } else {
                // 初始化成功，之后可以调用startSpeaking方法
                // 注：有的开发者在onCreate方法中创建完合成对象之后马上就调用startSpeaking进行合成，
                // 正确的做法是将onCreate中的startSpeaking调用移至这里
            }
        }
    };

    /**
     * 合成回调监听。
     */
    private SynthesizerListener mTtsListener = new SynthesizerListener() {

        @Override
        public void onSpeakBegin() {

            //showTip("开始播放");
        }

        @Override
        public void onSpeakPaused() {

            //showTip("暂停播放");
        }

        @Override
        public void onSpeakResumed() {

            //showTip("继续播放");
        }

        @Override
        public void onBufferProgress(int percent, int beginPos, int endPos,
                                     String info) {
            // 合成进度
//            mPercentForBuffering = percent;
//            showTip(String.format(getString(R.string.tts_toast_format),
//                    mPercentForBuffering, mPercentForPlaying));
        }

        @Override
        public void onSpeakProgress(int percent, int beginPos, int endPos) {
            // 播放进度
//            mPercentForPlaying = percent;
//            showTip(String.format(getString(R.string.tts_toast_format),
//                    mPercentForBuffering, mPercentForPlaying));
//
//            SpannableStringBuilder style=new SpannableStringBuilder(texts);
//            if(!"henry".equals(voicer)||!"xiaoyan".equals(voicer)||
//                    !"xiaoyu".equals(voicer)||!"catherine".equals(voicer))
//                endPos++;
//            Log.e(TAG,"beginPos = "+beginPos +"  endPos = "+endPos);
//            style.setSpan(new BackgroundColorSpan(Color.RED),beginPos,endPos, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//            ((EditText) findViewById(R.id.tts_text)).setText(style);
        }

        @Override
        public void onCompleted(SpeechError error) {
            if (error == null) {
                //showTip("播放完成");
                sendtojs("speakComplete", "'ok'");
            } else if (error != null) {
               // showTip("合成错误:"+error.getPlainDescription(true));
                sendtojs("speakComplete", "'"+error.getPlainDescription(true)+"'");
            }
        }

        @Override
        public void onEvent(int eventType, int arg1, int arg2, Bundle obj) {
            // 以下代码用于获取与云端的会话id，当业务出错时将会话id提供给技术支持人员，可用于查询会话日志，定位出错原因
            // 若使用本地能力，会话id为null
            Log.e(TAG,"TTS Demo onEvent >>>"+eventType);
            if (SpeechEvent.EVENT_SESSION_ID == eventType) {
                String sid = obj.getString(SpeechEvent.KEY_EVENT_SESSION_ID);
                Log.d(TAG, "session id =" + sid);
            }
        }
    };

    /**
     * 参数设置
     * @return
     */
    private void setParam(){
        // 清空参数
        mTts.setParameter(SpeechConstant.PARAMS, null);
        // 根据合成引擎设置相应参数
        if(mEngineType.equals(SpeechConstant.TYPE_CLOUD)) {
            mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD);
            //onevent回调接口实时返回音频流数据
            //mTts.setParameter(SpeechConstant.TTS_DATA_NOTIFY, "1");
            // 设置在线合成发音人
            mTts.setParameter(SpeechConstant.VOICE_NAME, voicer);
            //设置合成语速
            mTts.setParameter(SpeechConstant.SPEED, String.valueOf(speed));
            //设置合成音调
            mTts.setParameter(SpeechConstant.PITCH, String.valueOf(pitch));
            //设置合成音量
            mTts.setParameter(SpeechConstant.VOLUME,  String.valueOf(volume));
        }else {
            mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_LOCAL);
            // 设置本地合成发音人 voicer为空，默认通过语记界面指定发音人。
            mTts.setParameter(SpeechConstant.VOICE_NAME, voicer);
            /**
             * TODO 本地合成不设置语速、音调、音量，默认使用语记设置
             * 开发者如需自定义参数，请参考在线合成参数设置
             */
        }
        //设置播放器音频流类型
        mTts.setParameter(SpeechConstant.STREAM_TYPE, "3");
        // 设置播放合成音频打断音乐播放，默认为true
        mTts.setParameter(SpeechConstant.KEY_REQUEST_FOCUS, "false");

        // 设置音频保存路径，保存音频格式支持pcm、wav，设置路径为sd卡请注意WRITE_EXTERNAL_STORAGE权限
        // 注：AUDIO_FORMAT参数语记需要更新版本才能生效
        mTts.setParameter(SpeechConstant.AUDIO_FORMAT, "pcm");
        mTts.setParameter(SpeechConstant.TTS_AUDIO_PATH, Environment.getExternalStorageDirectory()+"/msc/tts.pcm");
    }

    private void showTip(final String str) {
        mToast.setText(str);
        mToast.show();
    }

    public void onPause(){
        FlowerCollector.onPageEnd(TAG);
        FlowerCollector.onPause(MainActivity.sContent);
    }

    public void onResume()
    {
        //移动数据统计分析
        FlowerCollector.onResume(MainActivity.sContent);
        FlowerCollector.onPageStart(TAG);
    }

    private void sendtojs(String funName, String msg)
    {
        final int version = Build.VERSION.SDK_INT;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            MainActivity.webView.evaluateJavascript("javascript:"+funName+"("+msg+")", new ValueCallback<String>() {
                @Override
                public void onReceiveValue(String value) {
                    //此处为 js 返回的结果
                    // Toast.makeText(MainActivity.sContent, value, Toast.LENGTH_SHORT).show();
                }
            });
        }
        else
        {
            Toast.makeText(MainActivity.sContent, "您的手机系统版本太低了, 最低版本要求,4.4.0", Toast.LENGTH_SHORT).show();
        }
    }
}
